package top.tenebrous.yzl.chat.utils;


import top.tenebrous.yzl.basic.constant.Constants;
import top.tenebrous.yzl.chat.domain.system.SysMenu;
import top.tenebrous.yzl.chat.model.MenuResDto;

import java.util.*;

public class BaseHelper {


    public static List<MenuResDto> getMenuList(List<SysMenu> menuList) {
        //一级菜单
        List<MenuResDto> menuResDtoList1 = new ArrayList<>();
        //二级菜单
        List<MenuResDto> menuResDtoList2 = new ArrayList<>();
        //三级菜单
        List<MenuResDto> menuResDtoList3 = new ArrayList<>();
        //四级菜单
        List<MenuResDto> menuResDtoList4 = new ArrayList<>();
        //五级菜单
        List<MenuResDto> menuResDtoList5 = new ArrayList<>();
        //六级菜单
        List<MenuResDto> menuResDtoList6 = new ArrayList<>();
        if (null != menuList) {
            Map<Long, Long> menuMap = new HashMap<>();
            for (SysMenu menu : menuList) {
                /*排除隐藏菜单或者已经加过的菜单*/
                if (Constants.STRING_ZERO.equals(menu.getIsVisible()) || menuMap.containsKey(menu.getId())) {
                    continue;
                }
                menuMap.put(menu.getId(), menu.getId());
                MenuResDto menuResDto = new MenuResDto();
                menuResDto.setId(menu.getId());
                menuResDto.setMenuName(menu.getMenuName());
                menuResDto.setVueName(menu.getVueName());
                menuResDto.setMenuIcon(menu.getMenuIcon());
                menuResDto.setMenuLink(menu.getMenuLink());
                menuResDto.setBizType(menu.getBizType());
                menuResDto.setMenuLevel(menu.getMenuLevel());
                menuResDto.setMenuSort(menu.getMenuSort());
                menuResDto.setParentId(menu.getParentId());
                menuResDto.setPermitMark(menu.getPermitMark());
                menuResDto.setIsLeaf(menu.getIsLeaf());
                menuResDto.setIsVisible(menu.getIsVisible());
                if (Constants.STRING_ONE.equals(menu.getMenuLevel().toString())) {
                    menuResDtoList1.add(menuResDto);
                } else if (Constants.STRING_TWO.equals(menu.getMenuLevel().toString())) {
                    menuResDtoList2.add(menuResDto);
                } else if (Constants.STRING_THREE.equals(menu.getMenuLevel().toString())) {
                    menuResDtoList3.add(menuResDto);
                } else if (Constants.STRING_FOUR.equals(menu.getMenuLevel().toString())) {
                    menuResDtoList4.add(menuResDto);
                } else if (Constants.STRING_FIVE.equals(menu.getMenuLevel().toString())) {
                    menuResDtoList5.add(menuResDto);
                } else if (Constants.STRING_SIX.equals(menu.getMenuLevel().toString())) {
                    menuResDtoList6.add(menuResDto);
                }
            }
        }
        //组装二级菜单
        formatMenu(menuResDtoList1, menuResDtoList2);
        //组装三级菜单
        formatMenu(menuResDtoList2, menuResDtoList3);
        //组装四级菜单
        formatMenu(menuResDtoList3, menuResDtoList4);
        //组装五级菜单
        formatMenu(menuResDtoList4, menuResDtoList5);
        //组装六级菜单
        formatMenu(menuResDtoList5, menuResDtoList6);
        if (menuResDtoList1.isEmpty()) {
            return new ArrayList<>();
        }
        return menuResDtoList1;
    }

    /*
        组装6级菜单
     */
    private static void formatMenu(List<MenuResDto> list1, List<MenuResDto> list2) {
        Comparator<MenuResDto> comparator = Comparator.comparingInt(MenuResDto::getMenuSort);
        list1.sort((m1, m2) -> comparator.compare(m2, m1));
        list2.sort((m1, m2) -> comparator.compare(m2, m1));
        for (MenuResDto dto1 : list1) {
            for (MenuResDto dto2 : list2) {
                if (dto2.getParentId().equals(dto1.getId())) {
                    dto1.getSubMenuList().add(dto2);
                }
            }
            if (dto1.getSubMenuList().size() > 0) {
                dto1.setHasLeaf(Constants.STRING_ONE);//有子菜单
            } else {
                dto1.setHasLeaf(Constants.STRING_ZERO);//无子菜单
            }
        }
    }

}
